#include <sstream>
#include <xscript/xitem.h>
#include <xscript/xitemboolean.h>
#include <xscript/xitemfloat.h>
#include <xscript/xitemint32.h>
#include <xscript/xitemstring.h>
#include <xscript/xitemtoken.h>
#include <xscript/xitemlist.h>
#include <xscript/xitemdict.h>
#include <xscript/xitemvisitor.h>
#include <xscript/xtoken.h>
#include <xscript/xprint.h>

XItem::XItem() {
}

XItem::XItem(const std::wstring& name):
    itemName(name) {
}

std::shared_ptr<XItem> XItem::createFromToken(std::shared_ptr<XToken> token) {
    std::shared_ptr<XItem> item;
    if (token->asBoolean())
        item = std::make_shared<XItemBoolean>(token->asBoolean()->getValue());
    else if (token->asFloat())
        item = std::make_shared<XItemFloat>(token->asFloat()->getValue());
    else if (token->asInteger())
        item = std::make_shared<XItemInt32>(token->asInteger()->getValue());
    else if (token->asString()) {
        auto string = token->asString()->getValue();
        string.pop_back();
        string.erase(string.begin());
        item = std::make_shared<XItemString>(string);
    } else if (token->asVariable())
        item = std::make_shared<XItemToken>(token);

    if (item)
        item->setLineNumber(token->getLineNumber());

    return item;
}

void XItem::accept(std::shared_ptr<XItemVisitor> visitor) {
    (void)visitor;
}

std::wstring XItem::getItemTypeInfo() {
    std::wstringstream stream;
    stream << L"<" << getStringFromType() << ">";
    return stream.str();
}

std::wstring XItem::getAddress() {
    std::wstringstream stream;
    stream << L"address:" << std::hex << this;
    return stream.str();
}

std::wstring XItem::getItemInfo() {
    std::wstringstream stream;
    stream << L"pointer:" << std::hex << this;
    return stream.str();
}

void XItem::setName(const std::wstring& name) {
    if (name != getName()) {
        itemName = name;
    }
}
